<!--  -->
<template>
    <div>
<br>
<br>
<br>
<br>
<div class="ui segment m-opacity m-container-70">
    <div class="ui container m-margin-tb-max-plus" style="max-width:30em!important;">
        <div class="ui middle aligned center aligned grid">
            <div class="column">
                <h2 class="ui teal image header">
                    <div class="content">
                        管理员后台登陆
                    </div>
                </h2>
                <div class="ui large form">
                    <div class="ui stacked segment">
                        <div class="field">
                            <div class="ui left icon input">
                                <i class="user icon"></i>
                                <input type="text" v-model="username" placeholder="用户名" name="username">
                            </div>
                        </div>
                        <div class="field">
                            <div class="ui left icon input">
                                <i class="lock icon"></i>
                                <input type="password" v-model="password" placeholder="密码" name="password">
                            </div>
                        </div>
                        <button class="ui fluid large teal submit button" @click="submit">登录</button>
                    </div>
                </div>
                <div class="ui negative message" v-show="error">
                    <i class="close icon" @click="closeMsg"></i>
                    <div class="header">提示:</div>
                    <p>{{message}}</p>
                </div>
            </div>
        </div>
    </div>

</div>

    </div>
</template>

<script>
export default {
    data () {
        return {
            username:"",
            password:"",
            error:false,
            message:""
        };
    },
    methods:{
        submit(){
            //非空验证
            var boo = $('.ui.form').form('validate form');
            if(!boo)
                return;
            var _this = this
            _this.axios.post("/adminLogin",{
                username:_this.username,
                password:_this.password
            }).then(res =>{
                if(res.data.code === 200){
                    _this.$store.commit('setToken',res.data.token)
                    _this.$store.commit('setUser',res.data.user)
                    _this.$router.push({path:'/admin/blogs'})
                }else{
                    _this.message=res.data.msg
                    _this.error=true
                }
            }).catch(e =>{
                console.log(e)
            })
        },
        closeMsg(){
            this.error= false
        }
    },
    mounted(){
        $('.ui.form').form({
            fields:{
                username:{
                    identifier:'username',
                    rules:[{
                        type:'empty',
                        prompt:'请输入用户名'
                    }]
                },
                password:{
                    identifier:'password',
                    rules:[{
                        type:'empty',
                        prompt:'请输入密码'
                    }]
                }
            }
        })
    }
}

</script>
<style lang='scss' scoped>
</style>